<html>
<head>
  <script type="text/javascript" src="../../zepto.min.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
      $("body").append('<p>Main</p>');
  });
var data;
  function showMess(){
    $("body").append('<p>Main</p>');
  }
</script>
</head>
<body>
<script>
  var gui = require('nw.gui');
  var program = require('../../node_modules/commander');
  var net = require('net');
  //command line arguments.
  program
    .option('--auto', 'run app automatic')
    .option('-p, --port <port>', "set port used by socket")
    .parse([ 'node-webkit', 'nw-test' ].concat(gui.App.argv));

  var prot = 13013;
  if (program.port) port = program.port;


  var windows = [];
  var winattr = {x: 50, y: 50, width: 300, height: 300, 
    frame: false, toolbar: false};
  
  function next(win, arr) {
    winattr.x += 400;
    if (winattr.x > 1600) { winattr.x = 50; }
    arr.push(win);
  }

  function newWindow(){
    var win = gui.Window.open('test.html', winattr);
    next(win, windows);
    return win;
  }

  function newBrowserWindow() {
    var win = window.open('test.html', null, 'screenX='+winattr.x+
        ',screenY='+winattr.y+',width=400,height=300');
    win = gui.Window.get(win);
    next(win, windows);
    return win;
  }

  function fourTimes(f){
    for (var i = 0; i < 4; i++) f();   
  }

  function testWindows(){
    for (var i = 0; i < windows.length; i++){
			if (windows[i].test) windows[i].test();
    }
  }


  gui.Window.get().on('close', function() {
    for (var i = 0; i < windows.length; i++) windows[i].close(true);
    this.close(true);
  });


  if (program.auto){
    var client = net.connect({port: port});
    client.setEncoding('utf8');

    client.on('data', function(data) {
      if (data == 'newWindow') {
        var win = newWindow();
       
        win.on('loaded', function() {
          if (win.test) {
            win.test();

            if (win.isTestSuccess()) {
              client.write('ok');
            } else {
              client.write('call does not success');
            }

          } else {
            client.write('call does not exist');
          }// if (win.test)
          win.close();
        });
        
      }//if (data == 'newWindow')

      if (data == 'newBrowserWindow') {
        var win;
        try {
          win = newBrowserWindow();
        }
        catch (e){
          client.write('there is error in creating window');
          return;
        }      
        win.on('loaded', function(){
          if (win.test) {
            win.test();

            if (win.isTestSuccess()) {
              client.write('ok');
            } else {
              client.write('call does not success');
            }

          } else {
            client.write('call does not exist');
          }// if (win.test)
          win.close();
        });
        

      }//if (data == 'newBrowserWindow') 

    });
  }
  

</script>
<button onclick="fourTimes(newWindow)">New gui windows</button>
<button onclick="fourTimes(newBrowserWindow)">New browser windows</button><br>
<button onclick="testWindows()">Test windows</button>
<button onclick="gui.App.quit()">quit</button>
</body>
</html>
